package com.qianfeng.zhangwei.sybl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.gc.flashview.FlashView;
import com.gc.flashview.listener.FlashViewListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.ActivityAdapter;
import com.qianfeng.zhangwei.sybl.adapter.ImageAndTextAdapter;
import com.qianfeng.zhangwei.sybl.bean.Activities;
import com.qianfeng.zhangwei.sybl.bean.Banner;
import com.qianfeng.zhangwei.sybl.bean.Entity;
import com.qianfeng.zhangwei.sybl.bean.H5HotCate;
import com.qianfeng.zhangwei.sybl.config.UrlContants;
import com.qianfeng.zhangwei.sybl.utils.AsyncHttpUtils;

import java.util.ArrayList;

/**
 * 小游戏
 *
 *
 *
 *
 *
 *
 * ScrollView  + listview  gridview
 * 第一个问题
 * 内容显示不全
 * 事件冲突 涉及到知识点  android事件分发的处理与消费
 */

public class GameFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView> {
    private PullToRefreshListView ptrListView;
    private ActivityAdapter adapter;
    private ArrayList<Activities> list = new ArrayList<>();
    private int page;
    private LayoutInflater inflater;
    private HeadHolder holder;

    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.fragment_base_ptrlistview);
        ptrListView.setOnRefreshListener(this);

        holder = new HeadHolder();
        ptrListView.getRefreshableView().addHeaderView(holder.view);

        adapter = new ActivityAdapter(list, activity);
        ptrListView.getRefreshableView().setAdapter(adapter);
        ptrListView.setRefreshing();
    }


    private void initData() {
        TypeReference<Entity<Activities>> type = new TypeReference<Entity<Activities>>() {
        };
        AsyncHttpUtils.getInstance().post(UrlContants.URL_ACTIVITY, type, getReqeustParams(), ptrListView, list, adapter, page);
    }


    private RequestParams getReqeustParams() {
        RequestParams params = new RequestParams();
        params.add("platform", "3");
        params.add("compare", "d0e2e22365013c957cdb5fe58e2c6617");
        return params;
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        page = 1;
        initData();
    }


    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        page++;
        initData();
    }
            
    private class HeadHolder implements AdapterView.OnItemClickListener, FlashViewListener {
        private View view;
        private FlashView flashView;
        private ArrayList<Banner> banners = new ArrayList<>();

        private ArrayList<String> imageUrls = new ArrayList<>();
        /**
         * 热门推荐
         */
        private GridView gridView;
        private ImageAndTextAdapter adapter;
        private ArrayList<H5HotCate> h5HotCates = new ArrayList<>();


        public HeadHolder() {
            view = inflater.inflate(R.layout.widget_game_head_layout, null);
            init();
            loadData();
        }


        private void init() {
            //自动轮播
            flashView = (FlashView) view.findViewById(R.id.game_flash_view);
            // 热门推荐
            gridView = (GridView) view.findViewById(R.id.game_gv);
            adapter = new ImageAndTextAdapter(h5HotCates, activity);
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(this);
            flashView.setOnPageClickListener(this);

        }


        private void loadData() {
            /**
             * 请求广告栏
             */
            TypeReference<Entity<H5HotCate>> reference = new TypeReference<Entity<H5HotCate>>() {
            };
            AsyncHttpUtils.getInstance().post(UrlContants.URL_GAME_HOT, reference, getHotCateParams(), h5HotCates, adapter);
            /**
             * 请求热门推荐
             */
            AsyncHttpUtils.getInstance().postBannerData(getBannerParams(), flashView,banners);


        }


        private RequestParams getHotCateParams() {
            RequestParams params = new RequestParams();
            params.put("platform", "2");
            params.put("compare", "2306d28d54c3261027a04d9049435295");
            return params;
        }

        private RequestParams getBannerParams() {
            RequestParams params = new RequestParams();
            params.put("platform", "2");
            params.put("compare", "2306d28d54c3261027a04d9049435295");
            params.put("pos", "3");
            return params;
        }


        @Override
        public void onClick(int position) {


        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    }


    /**
     * HeadView先关业务
     */
//    private class HeadHolder implements AdapterView.OnItemClickListener {
//        private View view;
//        private FlashView flashView;
//        private GridView gridView;
//        private ImageAndTextAdapter imageAndTextAdapter;
//        private ArrayList<H5HotCate> hotCates = new ArrayList<>();
//
//        public HeadHolder() {
//            view = inflater.inflate(R.layout.widget_game_head_layout, null);
//            flashView = (FlashView) view.findViewById(R.id.game_flash_view);
//            gridView = (GridView) view.findViewById(R.id.game_gv);
//            imageAndTextAdapter = new ImageAndTextAdapter(hotCates, activity);
//            gridView.setAdapter(imageAndTextAdapter);
//            gridView.setOnItemClickListener(this);
//            loadData();
//        }
//
//        private void loadData() {
//            /**
//             * 加载广告数据
//             */
//            AsyncHttpUtils.getInstance().postGameData(getBannerParams(), flashView);
//            /**
//             * 加载热门推荐数据
//             */
//            TypeReference<Entity<H5HotCate>> reference = new TypeReference<Entity<H5HotCate>>() {
//            };
//            AsyncHttpUtils.getInstance().post(UrlContants.URL_GAME_HOT, reference, getHotCateParams(), hotCates, imageAndTextAdapter, 0);
//        }
//
//
//        private RequestParams getHotCateParams() {
//            RequestParams params = new RequestParams();
//            params.put("platform", "2");
//            params.put("compare", "2306d28d54c3261027a04d9049435295");
//            return params;
//
//        }
//
//        private RequestParams getBannerParams() {
//            RequestParams params = new RequestParams();
//            params.put("platform", "2");
//            params.put("pos", "3");
//            params.put("compare", "54f3d5b6cd0b84fa56e3aff0de20068f");
//            return params;
//        }
//
//        @Override
//        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//        }
//    }

}
